// mutex example
#include <iostream>       // std::cout
#include <thread>         // std::thread
#include <mutex>          // std::mutex

std::mutex mtx;           // 互斥锁

void print_block (int n, char c) 
{
  // 通过锁定 MTX 发出对 std::cout 的独占访问
  mtx.lock(); // 加锁
  for (int i = 0; i < n; ++i) {
      std::cout << c; 
  }
  std::cout << '\n';
  mtx.unlock(); // 解锁
}

int main ()
{
  std::thread th1 (print_block, 50, '*');
  std::thread th2 (print_block, 50, '$');

  th1.join();
  th2.join();

  return 0;
}

